<template>
  <div class="sokoban-container">
    <div class="game-header">
      <h1>📦 推箱子</h1>
      <div class="game-stats">
        <div class="stat-card">
          <div class="stat-label">关卡</div>
          <div class="stat-value">{{ currentLevel }}</div>
        </div>
        <div class="stat-card">
          <div class="stat-label">步数</div>
          <div class="stat-value">{{ moves }}</div>
        </div>
        <div class="stat-card">
          <div class="stat-label">推箱</div>
          <div class="stat-value">{{ pushes }}</div>
        </div>
        <div class="stat-card">
          <div class="stat-label">最佳</div>
          <div class="stat-value">{{ getBestScore() }}</div>
        </div>
        <div class="stat-card">
          <div class="stat-label">完成</div>
          <div class="stat-value">{{ completedLevels }}/{{ totalLevels }}</div>
        </div>
      </div>
    </div>

    <div class="game-content">
      <!-- 左侧控制面板 -->
      <div class="left-panel">
        <div class="level-info">
          <h3>🎯 关卡信息</h3>
          <div class="level-details">
            <div class="level-number">第 {{ currentLevel }} 关</div>
            <div class="level-title">{{ getCurrentLevelTitle() }}</div>
            <div class="level-objective">
              <div class="objective-title">目标：</div>
              <div class="objective-text">将所有箱子推到目标位置</div>
            </div>
            <div class="level-stats">
              <div class="stat-row">
                <span class="stat-name">箱子数量：</span>
                <span class="stat-value">{{ getBoxCount() }}</span>
              </div>
              <div class="stat-row">
                <span class="stat-name">目标位置：</span>
                <span class="stat-value">{{ getTargetCount() }}</span>
              </div>
              <div class="stat-row">
                <span class="stat-name">已完成：</span>
                <span class="stat-value">{{ getCompletedBoxes() }}/{{ getBoxCount() }}</span>
              </div>
            </div>
          </div>
        </div>

        <div class="controls-section">
          <h3>🎮 游戏控制</h3>
          <div class="control-buttons">
            <button @click="startGame" :disabled="gameRunning" class="control-btn start">
              {{ gameRunning ? '游戏进行中' : '开始游戏' }}
            </button>
            <button @click="pauseGame" :disabled="!gameRunning" class="control-btn pause">
              {{ gamePaused ? '继续' : '暂停' }}
            </button>
            <button @click="resetLevel" class="control-btn reset">重置关卡</button>
            <button @click="undoMove" :disabled="!canUndo" class="control-btn undo">
              撤销 ({{ undoStack.length }})
            </button>
          </div>
        </div>

        <div class="level-selector">
          <h3>📋 关卡选择</h3>
          <div class="level-grid">
            <button
              v-for="level in totalLevels"
              :key="level"
              :class="[
                'level-btn',
                { 
                  current: level === currentLevel,
                  completed: isLevelCompleted(level),
                  locked: !isLevelUnlocked(level)
                }
              ]"
              @click="selectLevel(level)"
              :disabled="!isLevelUnlocked(level)"
            >
              <span class="level-number">{{ level }}</span>
              <span v-if="isLevelCompleted(level)" class="level-star">⭐</span>
            </button>
          </div>
        </div>

        <div class="controls-info">
          <h3>🕹️ 操作说明</h3>
          <div class="control-item">
            <span class="key">WASD</span>
            <span class="desc">移动角色</span>
          </div>
          <div class="control-item">
            <span class="key">方向键</span>
            <span class="desc">移动角色</span>
          </div>
          <div class="control-item">
            <span class="key">Z</span>
            <span class="desc">撤销上一步</span>
          </div>
          <div class="control-item">
            <span class="key">R</span>
            <span class="desc">重置关卡</span>
          </div>
          <div class="control-item">
            <span class="key">P</span>
            <span class="desc">暂停游戏</span>
          </div>
        </div>
      </div>

      <!-- 游戏主区域 -->
      <div class="game-main">
        <div class="game-board-wrapper">
          <div 
            class="game-board"
            :class="{ disabled: !gameRunning || gamePaused }"
            :style="{ 
              gridTemplateColumns: `repeat(${boardWidth}, 40px)`,
              gridTemplateRows: `repeat(${boardHeight}, 40px)`
            }"
            @keydown="handleKeyDown"
            tabindex="0"
            ref="gameBoard"
          >
            <div
              v-for="(cell, index) in flatBoard"
              :key="index"
              :class="[
                'cell',
                `cell-${cell.type}`,
                { 
                  'has-player': cell.hasPlayer,
                  'has-box': cell.hasBox,
                  'is-target': cell.isTarget,
                  'box-on-target': cell.hasBox && cell.isTarget,
                  'player-on-target': cell.hasPlayer && cell.isTarget
                }
              ]"
            >
              <!-- 地面和墙壁 -->
              <div v-if="cell.type === 'wall'" class="wall">🧱</div>
              <div v-else-if="cell.type === 'floor'" class="floor"></div>
              
              <!-- 目标位置 -->
              <div v-if="cell.isTarget" class="target">🎯</div>
              
              <!-- 箱子 -->
              <div v-if="cell.hasBox" class="box" :class="{ 'on-target': cell.isTarget }">
                {{ cell.isTarget ? '✅' : '📦' }}
              </div>
              
              <!-- 玩家 -->
              <div v-if="cell.hasPlayer" class="player" :class="{ 'on-target': cell.isTarget }">
                {{ getPlayerIcon() }}
              </div>
            </div>
          </div>

          <!-- 游戏状态覆盖层 -->
          <div v-if="!gameRunning && !gameOver && !levelComplete" class="game-overlay start-overlay">
            <div class="overlay-content">
              <h2>📦 准备开始</h2>
              <p>欢迎来到推箱子世界！</p>
              <p>将所有箱子推到目标位置即可过关</p>
              <p>使用WASD或方向键控制角色移动</p>
              <button @click="startGame" class="overlay-btn">开始游戏</button>
            </div>
          </div>

          <div v-if="gamePaused" class="game-overlay pause-overlay">
            <div class="overlay-content">
              <h2>⏸️ 游戏暂停</h2>
              <p>点击继续按钮恢复游戏</p>
              <button @click="pauseGame" class="overlay-btn">继续游戏</button>
            </div>
          </div>

          <div v-if="levelComplete" class="game-overlay level-complete-overlay">
            <div class="overlay-content">
              <h2>🎊 关卡完成！</h2>
              <p>成功通过第 {{ currentLevel }} 关</p>
              <div class="completion-stats">
                <div class="stat-item">
                  <span class="stat-label">步数：</span>
                  <span class="stat-value">{{ moves }}</span>
                </div>
                <div class="stat-item">
                  <span class="stat-label">推箱：</span>
                  <span class="stat-value">{{ pushes }}</span>
                </div>
                <div class="stat-item">
                  <span class="stat-label">用时：</span>
                  <span class="stat-value">{{ formatTime(gameTime) }}</span>
                </div>
              </div>
              <div class="performance-rating">
                <div class="rating-stars">
                  <div class="star" v-for="i in 3" :key="i" :class="{ filled: i <= getPerformanceRating() }">⭐</div>
                </div>
                <div class="rating-text">{{ getPerformanceText() }}</div>
              </div>
              <div class="level-buttons">
                <button @click="resetLevel" class="overlay-btn secondary">重玩关卡</button>
                <button @click="nextLevel" :disabled="currentLevel >= totalLevels" class="overlay-btn">
                  {{ currentLevel >= totalLevels ? '已完成所有关卡' : '下一关' }}
                </button>
              </div>
            </div>
          </div>

          <div v-if="gameOver" class="game-overlay game-over-overlay">
            <div class="overlay-content">
              <h2>🎮 游戏结束</h2>
              <p>恭喜完成所有关卡！</p>
              <div class="final-stats">
                <div class="stat-item">
                  <span class="stat-label">完成关卡：</span>
                  <span class="stat-value">{{ completedLevels }}/{{ totalLevels }}</span>
                </div>
                <div class="stat-item">
                  <span class="stat-label">总步数：</span>
                  <span class="stat-value">{{ totalMoves }}</span>
                </div>
                <div class="stat-item">
                  <span class="stat-label">总推箱：</span>
                  <span class="stat-value">{{ totalPushes }}</span>
                </div>
              </div>
              <button @click="resetGame" class="overlay-btn">重新开始</button>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧信息面板 -->
      <div class="right-panel">
        <div class="game-elements">
          <h3>🎮 游戏元素</h3>
          <div class="element-list">
            <div class="element-item">
              <div class="element-icon">🧑</div>
              <div class="element-info">
                <div class="element-name">玩家</div>
                <div class="element-desc">可以移动和推箱子</div>
              </div>
            </div>
            <div class="element-item">
              <div class="element-icon">📦</div>
              <div class="element-info">
                <div class="element-name">箱子</div>
                <div class="element-desc">需要推到目标位置</div>
              </div>
            </div>
            <div class="element-item">
              <div class="element-icon">🎯</div>
              <div class="element-info">
                <div class="element-name">目标</div>
                <div class="element-desc">箱子的目标位置</div>
              </div>
            </div>
            <div class="element-item">
              <div class="element-icon">✅</div>
              <div class="element-info">
                <div class="element-name">完成</div>
                <div class="element-desc">箱子在目标位置</div>
              </div>
            </div>
            <div class="element-item">
              <div class="element-icon">🧱</div>
              <div class="element-info">
                <div class="element-name">墙壁</div>
                <div class="element-desc">不可通过的障碍</div>
              </div>
            </div>
          </div>
        </div>

        <div class="achievements">
          <h3>🏆 成就</h3>
          <div class="achievement-list">
            <div 
              v-for="achievement in achievements"
              :key="achievement.id"
              :class="['achievement', { unlocked: achievement.unlocked }]"
            >
              <div class="achievement-icon">{{ achievement.icon }}</div>
              <div class="achievement-text">
                <div class="achievement-name">{{ achievement.name }}</div>
                <div class="achievement-desc">{{ achievement.description }}</div>
                <div v-if="achievement.unlocked" class="achievement-progress">已解锁</div>
                <div v-else class="achievement-progress">{{ achievement.progress }}/{{ achievement.target }}</div>
              </div>
            </div>
          </div>
        </div>

        <div class="game-tips">
          <h3>💡 游戏技巧</h3>
          <div class="tip-list">
            <div class="tip-item">先规划路线，避免箱子被卡住</div>
            <div class="tip-item">利用墙壁作为支撑点推箱子</div>
            <div class="tip-item">善用撤销功能纠正错误</div>
            <div class="tip-item">从最难移动的箱子开始</div>
            <div class="tip-item">保持通道畅通，避免堵塞</div>
          </div>
        </div>

        <div class="level-progress">
          <h3>📈 进度统计</h3>
          <div class="progress-info">
            <div class="progress-bar">
              <div class="progress-label">关卡进度</div>
              <div class="progress-track">
                <div 
                  class="progress-fill" 
                  :style="{ width: (completedLevels / totalLevels) * 100 + '%' }"
                ></div>
              </div>
              <div class="progress-text">{{ Math.floor((completedLevels / totalLevels) * 100) }}%</div>
            </div>
            <div class="time-display">
              <div class="time-label">游戏时间</div>
              <div class="time-value">{{ formatTime(gameTime) }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted, onUnmounted, computed, nextTick } from 'vue'

export default {
  name: 'SokobanGame',
  setup() {
    // 游戏状态
    const gameRunning = ref(false)
    const gamePaused = ref(false)
    const gameOver = ref(false)
    const levelComplete = ref(false)
    const currentLevel = ref(1)
    const moves = ref(0)
    const pushes = ref(0)
    const gameTime = ref(0)
    const totalMoves = ref(0)
    const totalPushes = ref(0)
    const completedLevels = ref(0)
    const totalLevels = ref(20)
    
    // 游戏板状态
    const board = ref([])
    const boardWidth = ref(0)
    const boardHeight = ref(0)
    const playerPos = ref({ x: 0, y: 0 })
    const undoStack = ref([])
    const gameBoard = ref(null)
    
    // 计时器
    let gameTimer = null
    
    // 关卡数据
    const levels = [
      // 第1关 - 简单入门
      {
        title: "初学者",
        width: 8,
        height: 6,
        data: [
          "########",
          "#      #",
          "# @$   #",
          "#    * #",
          "#      #",
          "########"
        ]
      },
      // 第2关 - 两个箱子
      {
        title: "双箱挑战",
        width: 9,
        height: 7,
        data: [
          "#########",
          "#       #",
          "# @$$   #",
          "#       #",
          "#       #",
          "#    ** #",
          "#########"
        ]
      },
      // 第3关 - L型推箱
      {
        title: "转角技巧",
        width: 10,
        height: 8,
        data: [
          "##########",
          "#        #",
          "# @      #",
          "#   $    #",
          "#   #    #",
          "#   #  * #",
          "#      * #",
          "##########"
        ]
      },
      // 第4关 - 狭窄通道
      {
        title: "狭窄通道",
        width: 11,
        height: 7,
        data: [
          "###########",
          "#    #    #",
          "# @$ # $  #",
          "#    #  * #",
          "#    #  * #",
          "#         #",
          "###########"
        ]
      },
      // 第5关 - 多箱子排列
      {
        title: "整齐排列",
        width: 12,
        height: 8,
        data: [
          "############",
          "#          #",
          "# @        #",
          "#  $$$     #",
          "#          #",
          "#     ***  #",
          "#          #",
          "############"
        ]
      },
      // 第6关 - 复杂迷宫
      {
        title: "迷宫探索",
        width: 13,
        height: 9,
        data: [
          "#############",
          "#     #     #",
          "# @$  #  $  #",
          "#  #  #  #  #",
          "#  #     #  #",
          "#  #  *  #  #",
          "#     #  *  #",
          "#     #     #",
          "#############"
        ]
      },
      // 第7关 - 推箱顺序
      {
        title: "顺序推箱",
        width: 10,
        height: 10,
        data: [
          "##########",
          "#        #",
          "# @      #",
          "#  $     #",
          "#   $    #",
          "#    $   #",
          "#     *  #",
          "#      * #",
          "#       *#",
          "##########"
        ]
      },
      // 第8关 - 围墙挑战
      {
        title: "围墙挑战",
        width: 11,
        height: 9,
        data: [
          "###########",
          "#         #",
          "# @  ###  #",
          "#  $ # $  #",
          "#  # # #  #",
          "#  $ # $  #",
          "#  **#**  #",
          "#         #",
          "###########"
        ]
      },
      // 第9关 - 交叉路口
      {
        title: "交叉路口",
        width: 12,
        height: 10,
        data: [
          "############",
          "#          #",
          "#    ##    #",
          "# @$ ## $  #",
          "#    ##    #",
          "#    ##    #",
          "#  * ## *  #",
          "#    ##    #",
          "#          #",
          "############"
        ]
      },
      // 第10关 - 高级挑战
      {
        title: "高级挑战",
        width: 14,
        height: 11,
        data: [
          "##############",
          "#            #",
          "# @    ####  #",
          "#  $$  #  $  #",
          "#   #  #  #  #",
          "#   #     #  #",
          "#   #  *  #  #",
          "#   #  *  #  #",
          "#      *     #",
          "#            #",
          "##############"
        ]
      },
      // 第11-20关可以继续添加更多复杂关卡...
      // 为了演示，我先添加几个基础关卡
      {
        title: "双重陷阱",
        width: 9,
        height: 8,
        data: [
          "#########",
          "#   #   #",
          "# @ # $ #",
          "#   #   #",
          "### ### #",
          "#  $ $  #",
          "#  * *  #",
          "#########"
        ]
      },
      {
        title: "螺旋迷宫",
        width: 11,
        height: 11,
        data: [
          "###########",
          "#         #",
          "# ####### #",
          "# #     # #",
          "# # ### # #",
          "# # #@  # #",
          "# # # $ # #",
          "# #   # # #",
          "# ##### # #",
          "#     * # #",
          "###########"
        ]
      },
      {
        title: "四角推箱",
        width: 10,
        height: 10,
        data: [
          "##########",
          "#$      $#",
          "#        #",
          "#        #",
          "#   @    #",
          "#        #",
          "#        #",
          "#        #",
          "#*      *#",
          "##########"
        ]
      },
      {
        title: "连环推箱",
        width: 12,
        height: 8,
        data: [
          "############",
          "#          #",
          "# @$$$$$$  #",
          "#          #",
          "#          #",
          "#  ******  #",
          "#          #",
          "############"
        ]
      },
      {
        title: "终极挑战",
        width: 15,
        height: 12,
        data: [
          "###############",
          "#             #",
          "# ########### #",
          "# #         # #",
          "# # ####### # #",
          "# # #  @  # # #",
          "# # # $$$ # # #",
          "# # # *** # # #",
          "# # ####### # #",
          "# #         # #",
          "# ########### #",
          "###############"
        ]
      },
      // 继续添加更多关卡...
      {
        title: "大师级别",
        width: 16,
        height: 12,
        data: [
          "################",
          "#              #",
          "# @  ########  #",
          "#  $ #      #  #",
          "# $$ #  ##  # $#",
          "#  # #  ##  # $#",
          "#  # #      #  #",
          "#  # ######## ##",
          "#  $         $ #",
          "#  **      **  #",
          "#              #",
          "################"
        ]
      },
      {
        title: "完美收官",
        width: 14,
        height: 14,
        data: [
          "##############",
          "#            #",
          "# ########## #",
          "# #        # #",
          "# # ###### # #",
          "# # #    # # #",
          "# # # @$ # # #",
          "# # # $* # # #",
          "# # #    # # #",
          "# # ###### # #",
          "# #        # #",
          "# ########## #",
          "#            #",
          "##############"
        ]
      },
      {
        title: "传奇关卡",
        width: 18,
        height: 14,
        data: [
          "##################",
          "#                #",
          "# ############## #",
          "# #            # #",
          "# # ########## # #",
          "# # #        # # #",
          "# # # ###### # # #",
          "# # # # @$$# # # #",
          "# # # # **## # # #",
          "# # # ###### # # #",
          "# # #        # # #",
          "# # ########## # #",
          "# #            # #",
          "##################"
        ]
      },
      {
        title: "最终Boss",
        width: 20,
        height: 15,
        data: [
          "####################",
          "#                  #",
          "# ################ #",
          "# #              # #",
          "# # ############ # #",
          "# # #          # # #",
          "# # # ######## # # #",
          "# # # #   @  # # # #",
          "# # # # $$$$ # # # #",
          "# # # # **** # # # #",
          "# # # ######## # # #",
          "# # #          # # #",
          "# # ############ # #",
          "# #              # #",
          "####################"
        ]
      }
    ]
    
    // 成就系统
    const achievements = ref([
      { id: 1, name: '初学者', description: '完成第1关', icon: '🎯', unlocked: false, progress: 0, target: 1 },
      { id: 2, name: '推箱高手', description: '完成10关', icon: '📦', unlocked: false, progress: 0, target: 10 },
      { id: 3, name: '效率专家', description: '用最少步数完成关卡', icon: '⚡', unlocked: false, progress: 0, target: 5 },
      { id: 4, name: '撤销大师', description: '使用撤销功能100次', icon: '↩️', unlocked: false, progress: 0, target: 100 },
      { id: 5, name: '推箱传奇', description: '完成所有关卡', icon: '👑', unlocked: false, progress: 0, target: 20 }
    ])
    
    // 计算属性
    const flatBoard = computed(() => {
      const flat = []
      for (let y = 0; y < boardHeight.value; y++) {
        for (let x = 0; x < boardWidth.value; x++) {
          if (board.value[y] && board.value[y][x]) {
            flat.push(board.value[y][x])
          }
        }
      }
      return flat
    })
    
    const canUndo = computed(() => {
      return undoStack.value.length > 0 && gameRunning.value && !gamePaused.value
    })
    
    // 初始化关卡
    const initializeLevel = (levelIndex) => {
      const level = levels[levelIndex - 1]
      if (!level) return
      
      boardWidth.value = level.width
      boardHeight.value = level.height
      board.value = []
      
      for (let y = 0; y < level.height; y++) {
        board.value[y] = []
        for (let x = 0; x < level.width; x++) {
          const char = level.data[y] && level.data[y][x] ? level.data[y][x] : ' '
          const cell = {
            type: char === '#' ? 'wall' : 'floor',
            hasPlayer: char === '@' || char === '+',
            hasBox: char === '$' || char === '*',
            isTarget: char === '.' || char === '*' || char === '+',
            x,
            y
          }
          
          if (cell.hasPlayer) {
            playerPos.value = { x, y }
          }
          
          board.value[y][x] = cell
        }
      }
      
      // 重置游戏状态
      moves.value = 0
      pushes.value = 0
      gameTime.value = 0
      undoStack.value = []
      levelComplete.value = false
    }
    
    // 保存游戏状态（用于撤销）
    const saveGameState = () => {
      const state = {
        board: JSON.parse(JSON.stringify(board.value)),
        playerPos: { ...playerPos.value },
        moves: moves.value,
        pushes: pushes.value
      }
      undoStack.value.push(state)
      
      // 限制撤销栈大小
      if (undoStack.value.length > 50) {
        undoStack.value.shift()
      }
    }
    
    // 撤销移动
    const undoMove = () => {
      if (!canUndo.value) return
      
      const state = undoStack.value.pop()
      if (state) {
        board.value = state.board
        playerPos.value = state.playerPos
        moves.value = state.moves
        pushes.value = state.pushes
        
        // 更新成就
        achievements.value[3].progress++
        if (achievements.value[3].progress >= achievements.value[3].target) {
          achievements.value[3].unlocked = true
        }
      }
    }
    
    // 移动玩家
    const movePlayer = (dx, dy) => {
      if (!gameRunning.value || gamePaused.value || levelComplete.value) return
      
      const newX = playerPos.value.x + dx
      const newY = playerPos.value.y + dy
      
      // 检查边界
      if (newX < 0 || newX >= boardWidth.value || newY < 0 || newY >= boardHeight.value) return
      
      const targetCell = board.value[newY][newX]
      
      // 检查墙壁
      if (targetCell.type === 'wall') return
      
      // 保存状态用于撤销
      saveGameState()
      
      // 检查是否推箱子
      if (targetCell.hasBox) {
        const boxNewX = newX + dx
        const boxNewY = newY + dy
        
        // 检查箱子目标位置
        if (boxNewX < 0 || boxNewX >= boardWidth.value || boxNewY < 0 || boxNewY >= boardHeight.value) {
          undoStack.value.pop() // 移除刚保存的状态
          return
        }
        
        const boxTargetCell = board.value[boxNewY][boxNewX]
        
        // 箱子不能推到墙上或另一个箱子上
        if (boxTargetCell.type === 'wall' || boxTargetCell.hasBox) {
          undoStack.value.pop() // 移除刚保存的状态
          return
        }
        
        // 移动箱子
        targetCell.hasBox = false
        boxTargetCell.hasBox = true
        pushes.value++
        totalPushes.value++
      }
      
      // 移动玩家
      board.value[playerPos.value.y][playerPos.value.x].hasPlayer = false
      board.value[newY][newX].hasPlayer = true
      playerPos.value = { x: newX, y: newY }
      
      moves.value++
      totalMoves.value++
      
      // 检查胜利条件
      checkWinCondition()
    }
    
    // 检查胜利条件
    const checkWinCondition = () => {
      let totalBoxes = 0
      let totalTargets = 0
      let boxesOnTargets = 0
      
      // 统计箱子、目标和在目标位置的箱子数量
      for (let y = 0; y < boardHeight.value; y++) {
        for (let x = 0; x < boardWidth.value; x++) {
          const cell = board.value[y][x]
          
          if (cell.hasBox) {
            totalBoxes++
            if (cell.isTarget) {
              boxesOnTargets++
            }
          }
          
          if (cell.isTarget) {
            totalTargets++
          }
        }
      }
      
      // 胜利条件：所有箱子都在目标位置，且箱子数量等于目标数量
      const isWin = totalBoxes === totalTargets && boxesOnTargets === totalTargets && totalBoxes > 0
      
      if (isWin) {
        levelComplete.value = true
        gameRunning.value = false
        
        // 更新完成关卡数
        if (!isLevelCompleted(currentLevel.value)) {
          completedLevels.value++
        }
        
        // 保存最佳成绩
        saveBestScore()
        
        // 检查成就
        checkAchievements()
        
        // 检查是否完成所有关卡
        if (completedLevels.value >= totalLevels.value) {
          setTimeout(() => {
            gameOver.value = true
          }, 2000)
        }
      }
    }
    
    // 键盘事件处理
    const handleKeyDown = (event) => {
      if (!gameRunning.value || gamePaused.value) return
      
      switch (event.key.toLowerCase()) {
        case 'w':
        case 'arrowup':
          event.preventDefault()
          movePlayer(0, -1)
          break
        case 's':
        case 'arrowdown':
          event.preventDefault()
          movePlayer(0, 1)
          break
        case 'a':
        case 'arrowleft':
          event.preventDefault()
          movePlayer(-1, 0)
          break
        case 'd':
        case 'arrowright':
          event.preventDefault()
          movePlayer(1, 0)
          break
        case 'z':
          event.preventDefault()
          undoMove()
          break
        case 'r':
          event.preventDefault()
          resetLevel()
          break
        case 'p':
          event.preventDefault()
          pauseGame()
          break
      }
    }
    
    // 获取玩家图标
    const getPlayerIcon = () => {
      return '🧑'
    }
    
    // 获取当前关卡标题
    const getCurrentLevelTitle = () => {
      const level = levels[currentLevel.value - 1]
      return level ? level.title : '未知关卡'
    }
    
    // 获取箱子数量
    const getBoxCount = () => {
      let count = 0
      for (let y = 0; y < boardHeight.value; y++) {
        for (let x = 0; x < boardWidth.value; x++) {
          if (board.value[y] && board.value[y][x] && board.value[y][x].hasBox) {
            count++
          }
        }
      }
      return count
    }
    
    // 获取目标位置数量
    const getTargetCount = () => {
      let count = 0
      for (let y = 0; y < boardHeight.value; y++) {
        for (let x = 0; x < boardWidth.value; x++) {
          if (board.value[y] && board.value[y][x] && board.value[y][x].isTarget) {
            count++
          }
        }
      }
      return count
    }
    
    // 获取已完成的箱子数量
    const getCompletedBoxes = () => {
      let count = 0
      for (let y = 0; y < boardHeight.value; y++) {
        for (let x = 0; x < boardWidth.value; x++) {
          const cell = board.value[y] && board.value[y][x]
          if (cell && cell.hasBox && cell.isTarget) {
            count++
          }
        }
      }
      return count
    }
    
    // 获取最佳成绩
    const getBestScore = () => {
      const best = localStorage.getItem(`sokoban-best-${currentLevel.value}`)
      return best ? JSON.parse(best).moves : '-'
    }
    
    // 保存最佳成绩
    const saveBestScore = () => {
      const key = `sokoban-best-${currentLevel.value}`
      const current = localStorage.getItem(key)
      const currentBest = current ? JSON.parse(current) : null
      
      if (!currentBest || moves.value < currentBest.moves) {
        const score = {
          moves: moves.value,
          pushes: pushes.value,
          time: gameTime.value
        }
        localStorage.setItem(key, JSON.stringify(score))
      }
    }
    
    // 检查关卡是否完成
    const isLevelCompleted = (level) => {
      return localStorage.getItem(`sokoban-completed-${level}`) === 'true'
    }
    
    // 检查关卡是否解锁
    const isLevelUnlocked = (level) => {
      if (level === 1) return true
      return isLevelCompleted(level - 1)
    }
    
    // 标记关卡完成
    const markLevelCompleted = (level) => {
      localStorage.setItem(`sokoban-completed-${level}`, 'true')
    }
    
    // 获取表现评级
    const getPerformanceRating = () => {
      const best = getBestScore()
      if (best === '-') return 3
      
      const ratio = moves.value / best
      if (ratio <= 1.1) return 3
      if (ratio <= 1.3) return 2
      return 1
    }
    
    // 获取表现文本
    const getPerformanceText = () => {
      const rating = getPerformanceRating()
      switch (rating) {
        case 3: return '完美表现！'
        case 2: return '表现良好！'
        case 1: return '还有进步空间'
        default: return '继续努力！'
      }
    }
    
    // 格式化时间
    const formatTime = (seconds) => {
      const mins = Math.floor(seconds / 60)
      const secs = seconds % 60
      return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
    }
    
    // 检查成就
    const checkAchievements = () => {
      // 初学者：完成第1关
      if (currentLevel.value >= 1 && !achievements.value[0].unlocked) {
        achievements.value[0].unlocked = true
        achievements.value[0].progress = 1
      }
      
      // 推箱高手：完成10关
      if (completedLevels.value >= 10 && !achievements.value[1].unlocked) {
        achievements.value[1].unlocked = true
        achievements.value[1].progress = completedLevels.value
      }
      
      // 效率专家：用最少步数完成关卡
      const best = getBestScore()
      if (best !== '-' && moves.value <= best && !achievements.value[2].unlocked) {
        achievements.value[2].progress++
        if (achievements.value[2].progress >= achievements.value[2].target) {
          achievements.value[2].unlocked = true
        }
      }
      
      // 推箱传奇：完成所有关卡
      if (completedLevels.value >= totalLevels.value && !achievements.value[4].unlocked) {
        achievements.value[4].unlocked = true
        achievements.value[4].progress = completedLevels.value
      }
      
      // 更新进度
      achievements.value[1].progress = Math.max(achievements.value[1].progress, completedLevels.value)
      achievements.value[4].progress = Math.max(achievements.value[4].progress, completedLevels.value)
    }
    
    // 游戏控制方法
    const startGame = () => {
      if (gameRunning.value) return
      
      gameRunning.value = true
      gamePaused.value = false
      gameOver.value = false
      levelComplete.value = false
      
      // 开始计时
      gameTimer = setInterval(() => {
        if (gameRunning.value && !gamePaused.value) {
          gameTime.value++
        }
      }, 1000)
      
      // 聚焦游戏板以接收键盘事件
      nextTick(() => {
        if (gameBoard.value) {
          gameBoard.value.focus()
        }
      })
    }
    
    const pauseGame = () => {
      if (!gameRunning.value) return
      gamePaused.value = !gamePaused.value
    }
    
    const resetLevel = () => {
      initializeLevel(currentLevel.value)
      levelComplete.value = false
      
      if (gameRunning.value) {
        nextTick(() => {
          if (gameBoard.value) {
            gameBoard.value.focus()
          }
        })
      }
    }
    
    const selectLevel = (level) => {
      if (!isLevelUnlocked(level)) return
      
      currentLevel.value = level
      initializeLevel(level)
      
      if (gameRunning.value) {
        nextTick(() => {
          if (gameBoard.value) {
            gameBoard.value.focus()
          }
        })
      }
    }
    
    const nextLevel = () => {
      if (currentLevel.value >= totalLevels.value) return
      
      // 标记当前关卡完成
      markLevelCompleted(currentLevel.value)
      
      currentLevel.value++
      initializeLevel(currentLevel.value)
      levelComplete.value = false
      
      if (gameRunning.value) {
        nextTick(() => {
          if (gameBoard.value) {
            gameBoard.value.focus()
          }
        })
      }
    }
    
    const resetGame = () => {
      gameRunning.value = false
      gamePaused.value = false
      gameOver.value = false
      levelComplete.value = false
      currentLevel.value = 1
      totalMoves.value = 0
      totalPushes.value = 0
      completedLevels.value = 0
      
      // 清除计时器
      if (gameTimer) {
        clearInterval(gameTimer)
        gameTimer = null
      }
      
      // 重置成就
      achievements.value.forEach(achievement => {
        achievement.unlocked = false
        achievement.progress = 0
      })
      
      // 清除本地存储
      for (let i = 1; i <= totalLevels.value; i++) {
        localStorage.removeItem(`sokoban-completed-${i}`)
        localStorage.removeItem(`sokoban-best-${i}`)
      }
      
      initializeLevel(1)
    }
    
    // 生命周期
    onMounted(() => {
      // 加载完成的关卡数
      for (let i = 1; i <= totalLevels.value; i++) {
        if (isLevelCompleted(i)) {
          completedLevels.value++
        }
      }
      
      // 加载成就进度
      const undoCount = parseInt(localStorage.getItem('sokoban-undo-count') || '0')
      achievements.value[3].progress = undoCount
      if (undoCount >= achievements.value[3].target) {
        achievements.value[3].unlocked = true
      }
      
      initializeLevel(1)
      
      // 添加全局键盘事件监听
      document.addEventListener('keydown', handleKeyDown)
    })
    
    onUnmounted(() => {
      // 清除计时器
      if (gameTimer) {
        clearInterval(gameTimer)
      }
      
      // 保存撤销次数
      localStorage.setItem('sokoban-undo-count', achievements.value[3].progress.toString())
      
      // 移除键盘事件监听
      document.removeEventListener('keydown', handleKeyDown)
    })
    
    return {
      // 响应式数据
      gameRunning,
      gamePaused,
      gameOver,
      levelComplete,
      currentLevel,
      moves,
      pushes,
      gameTime,
      totalMoves,
      totalPushes,
      completedLevels,
      totalLevels,
      board,
      boardWidth,
      boardHeight,
      playerPos,
      undoStack,
      gameBoard,
      achievements,
      flatBoard,
      canUndo,
      
      // 方法
      startGame,
      pauseGame,
      resetLevel,
      selectLevel,
      nextLevel,
      resetGame,
      undoMove,
      handleKeyDown,
      getPlayerIcon,
      getCurrentLevelTitle,
      getBoxCount,
      getTargetCount,
      getCompletedBoxes,
      getBestScore,
      isLevelCompleted,
      isLevelUnlocked,
      getPerformanceRating,
      getPerformanceText,
      formatTime
    }
  }
}
</script>

<style scoped>
.sokoban-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: linear-gradient(135deg, #8B4513 0%, #D2691E 50%, #F4A460 100%);
  min-height: 100vh;
  color: #333;
  position: relative;
}

.game-header {
  text-align: center;
  margin-bottom: 30px;
}

.game-header h1 {
  font-size: 3em;
  margin-bottom: 20px;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  background: linear-gradient(45deg, #8B4513, #D2691E, #F4A460, #DEB887);
  background-size: 400% 400%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: boxGlow 3s ease infinite;
}

@keyframes boxGlow {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

.game-stats {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
}

.stat-card {
  background: rgba(255,255,255,0.9);
  padding: 15px 20px;
  border-radius: 15px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255,255,255,0.3);
  text-align: center;
  min-width: 100px;
  box-shadow: 0 0 20px rgba(139,69,19,0.2);
}

.stat-label {
  font-size: 0.9em;
  opacity: 0.8;
  margin-bottom: 5px;
  color: #666;
}

.stat-value {
  font-size: 1.5em;
  font-weight: bold;
  color: #8B4513;
  text-shadow: 0 0 10px rgba(139,69,19,0.3);
}

.game-content {
  display: grid;
  grid-template-columns: 280px 1fr 280px;
  gap: 30px;
  align-items: start;
}

.left-panel, .right-panel {
  background: rgba(255,255,255,0.9);
  padding: 20px;
  border-radius: 15px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255,255,255,0.3);
  box-shadow: 0 0 20px rgba(139,69,19,0.1);
}

.level-info, .controls-section, .level-selector, .controls-info {
  margin-bottom: 25px;
}

.level-info h3, .controls-section h3, .level-selector h3, .controls-info h3 {
  text-align: center;
  margin-bottom: 15px;
  color: #8B4513;
  text-shadow: 0 0 10px rgba(139,69,19,0.3);
}

.level-number {
  font-size: 1.5em;
  font-weight: bold;
  text-align: center;
  color: #8B4513;
  margin-bottom: 5px;
}

.level-title {
  font-size: 1.1em;
  text-align: center;
  color: #D2691E;
  margin-bottom: 15px;
  font-style: italic;
}

.objective-title {
  font-weight: bold;
  margin-bottom: 5px;
  color: #333;
}

.objective-text {
  color: #666;
  margin-bottom: 15px;
}

.level-stats {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9em;
}

.stat-name {
  color: #666;
}

.stat-value {
  font-weight: bold;
  color: #8B4513;
}

.control-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.control-btn {
  padding: 12px 20px;
  border: none;
  border-radius: 25px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255,255,255,0.2);
}

.control-btn.start {
  background: linear-gradient(45deg, #32CD32, #228B22);
  color: white;
  box-shadow: 0 0 20px rgba(50,205,50,0.3);
}

.control-btn.pause {
  background: linear-gradient(45deg, #FFD700, #FFA500);
  color: white;
  box-shadow: 0 0 20px rgba(255,215,0,0.3);
}

.control-btn.reset {
  background: linear-gradient(45deg, #FF6347, #DC143C);
  color: white;
  box-shadow: 0 0 20px rgba(255,99,71,0.3);
}

.control-btn.undo {
  background: linear-gradient(45deg, #9370DB, #8A2BE2);
  color: white;
  box-shadow: 0 0 20px rgba(147,112,219,0.3);
}

.control-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 5px 25px rgba(0,0,0,0.3);
}

.control-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.level-btn {
  position: relative;
  padding: 12px 8px;
  border: 2px solid #ddd;
  border-radius: 10px;
  background: rgba(255,255,255,0.8);
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 0.9em;
}

.level-btn:hover:not(.locked) {
  border-color: #8B4513;
  background: rgba(139,69,19,0.1);
  transform: translateY(-2px);
}

.level-btn.current {
  border-color: #8B4513;
  background: rgba(139,69,19,0.2);
  box-shadow: 0 0 10px rgba(139,69,19,0.3);
}

.level-btn.completed {
  border-color: #32CD32;
  background: rgba(50,205,50,0.1);
}

.level-btn.locked {
  opacity: 0.5;
  cursor: not-allowed;
}

.level-number {
  font-weight: bold;
  color: #333;
}

.level-star {
  font-size: 0.8em;
  margin-top: 2px;
}

.control-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-size: 0.9em;
}

.control-item .key {
  background: #8B4513;
  color: white;
  padding: 4px 8px;
  border-radius: 6px;
  font-weight: bold;
  font-size: 0.8em;
}

.control-item .desc {
  color: #666;
}

.game-main {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.game-board-wrapper {
  position: relative;
  border: 3px solid #8B4513;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 0 30px rgba(139,69,19,0.5);
  background: rgba(255,255,255,0.9);
  padding: 20px;
}

.game-board {
  display: grid;
  gap: 1px;
  background: #654321;
  padding: 10px;
  border-radius: 10px;
  outline: none;
}

.game-board.disabled {
  pointer-events: none;
  opacity: 0.7;
}

.cell {
  width: 40px;
  height: 40px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.cell-wall {
  background: #8B4513;
  border: 2px solid #654321;
}

.cell-floor {
  background: #F5DEB3;
  border: 1px solid #DEB887;
}

.wall {
  font-size: 1.5em;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.3);
}

.floor {
  width: 100%;
  height: 100%;
  background: radial-gradient(circle, #F5DEB3, #DEB887);
}

.target {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 1.2em;
  opacity: 0.7;
  z-index: 1;
}

.box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 1.8em;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  z-index: 2;
  transition: all 0.3s ease;
}

.box.on-target {
  animation: boxSuccess 1s ease infinite alternate;
}

@keyframes boxSuccess {
  from { transform: translate(-50%, -50%) scale(1); }
  to { transform: translate(-50%, -50%) scale(1.1); }
}

.player {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 1.8em;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  z-index: 3;
  transition: all 0.3s ease;
}

.player.on-target {
  animation: playerGlow 2s ease infinite alternate;
}

@keyframes playerGlow {
  from { filter: brightness(1); }
  to { filter: brightness(1.3); }
}

.game-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 15px;
  backdrop-filter: blur(10px);
}

.overlay-content {
  background: rgba(255,255,255,0.95);
  padding: 40px;
  border-radius: 20px;
  text-align: center;
  box-shadow: 0 0 30px rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.3);
  max-width: 400px;
}

.overlay-content h2 {
  margin-bottom: 20px;
  color: #8B4513;
  text-shadow: 0 0 10px rgba(139,69,19,0.3);
}

.overlay-content p {
  margin-bottom: 15px;
  color: #666;
  line-height: 1.6;
}

.overlay-btn {
  padding: 15px 30px;
  background: linear-gradient(45deg, #8B4513, #D2691E);
  color: white;
  border: none;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 0 20px rgba(139,69,19,0.3);
  margin: 5px;
}

.overlay-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 25px rgba(139,69,19,0.5);
}

.overlay-btn.secondary {
  background: linear-gradient(45deg, #666, #888);
  box-shadow: 0 0 20px rgba(102,102,102,0.3);
}

.overlay-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.completion-stats, .final-stats {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 20px 0;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #eee;
}

.stat-label {
  color: #666;
}

.stat-value {
  font-weight: bold;
  color: #8B4513;
}

.performance-rating {
  margin: 20px 0;
  text-align: center;
}

.rating-stars {
  display: flex;
  justify-content: center;
  gap: 5px;
  margin-bottom: 10px;
}

.star {
  font-size: 1.5em;
  opacity: 0.3;
  transition: all 0.3s ease;
}

.star.filled {
  opacity: 1;
  animation: starGlow 2s ease infinite alternate;
}

@keyframes starGlow {
  from { transform: scale(1); }
  to { transform: scale(1.2); }
}

.rating-text {
  font-weight: bold;
  color: #8B4513;
}

.level-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 20px;
}

.game-elements, .achievements, .game-tips, .level-progress {
  margin-bottom: 25px;
}

.game-elements h3, .achievements h3, .game-tips h3, .level-progress h3 {
  text-align: center;
  margin-bottom: 15px;
  color: #8B4513;
  text-shadow: 0 0 10px rgba(139,69,19,0.3);
}

.element-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.element-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px;
  background: rgba(139,69,19,0.1);
  border-radius: 10px;
  border: 1px solid rgba(139,69,19,0.2);
}

.element-icon {
  font-size: 1.5em;
  width: 40px;
  text-align: center;
}

.element-info {
  flex: 1;
}

.element-name {
  font-weight: bold;
  color: #8B4513;
  margin-bottom: 2px;
}

.element-desc {
  font-size: 0.9em;
  color: #666;
}

.achievement-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.achievement {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid #ddd;
  background: rgba(255,255,255,0.5);
  transition: all 0.3s ease;
}

.achievement.unlocked {
  border-color: #32CD32;
  background: rgba(50,205,50,0.1);
  box-shadow: 0 0 10px rgba(50,205,50,0.2);
}

.achievement-icon {
  font-size: 1.5em;
  width: 40px;
  text-align: center;
}

.achievement-text {
  flex: 1;
}

.achievement-name {
  font-weight: bold;
  color: #8B4513;
  margin-bottom: 2px;
}

.achievement-desc {
  font-size: 0.9em;
  color: #666;
  margin-bottom: 4px;
}

.achievement-progress {
  font-size: 0.8em;
  color: #32CD32;
  font-weight: bold;
}

.tip-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tip-item {
  padding: 8px 12px;
  background: rgba(255,215,0,0.1);
  border-left: 3px solid #FFD700;
  border-radius: 5px;
  font-size: 0.9em;
  color: #666;
}

.progress-info {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.progress-bar {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.progress-label {
  font-weight: bold;
  color: #8B4513;
  text-align: center;
}

.progress-track {
  height: 20px;
  background: rgba(139,69,19,0.2);
  border-radius: 10px;
  overflow: hidden;
  position: relative;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(45deg, #32CD32, #228B22);
  border-radius: 10px;
  transition: width 0.5s ease;
  position: relative;
}

.progress-fill::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
  animation: progressShine 2s ease infinite;
}

@keyframes progressShine {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

.progress-text {
  text-align: center;
  font-weight: bold;
  color: #8B4513;
}

.time-display {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding: 15px;
  background: rgba(139,69,19,0.1);
  border-radius: 10px;
  border: 1px solid rgba(139,69,19,0.2);
}

.time-label {
  font-weight: bold;
  color: #8B4513;
}

.time-value {
  font-size: 1.5em;
  font-weight: bold;
  color: #D2691E;
  text-shadow: 0 0 10px rgba(210,105,30,0.3);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .game-content {
    grid-template-columns: 250px 1fr 250px;
    gap: 20px;
  }
  
  .left-panel, .right-panel {
    padding: 15px;
  }
}

@media (max-width: 1000px) {
  .game-content {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  .left-panel, .right-panel {
    order: 2;
  }
  
  .game-main {
    order: 1;
  }
  
  .level-grid {
    grid-template-columns: repeat(10, 1fr);
  }
}

@media (max-width: 768px) {
  .sokoban-container {
    padding: 10px;
  }
  
  .game-header h1 {
    font-size: 2em;
  }
  
  .game-stats {
    gap: 10px;
  }
  
  .stat-card {
    padding: 10px 15px;
    min-width: 80px;
  }
  
  .stat-value {
    font-size: 1.2em;
  }
  
  .game-board-wrapper {
    padding: 10px;
  }
  
  .cell {
    width: 30px;
    height: 30px;
  }
  
  .game-board {
    grid-template-columns: repeat(var(--board-width), 30px) !important;
    grid-template-rows: repeat(var(--board-height), 30px) !important;
  }
  
  .overlay-content {
    padding: 20px;
    margin: 10px;
  }
  
  .level-grid {
    grid-template-columns: repeat(5, 1fr);
  }
}

@media (max-width: 480px) {
  .game-header h1 {
    font-size: 1.5em;
  }
  
  .game-stats {
    flex-direction: column;
    align-items: center;
  }
  
  .cell {
    width: 25px;
    height: 25px;
  }
  
  .game-board {
    grid-template-columns: repeat(var(--board-width), 25px) !important;
    grid-template-rows: repeat(var(--board-height), 25px) !important;
  }
  
  .wall, .box, .player {
    font-size: 1em;
  }
  
  .target {
    font-size: 0.8em;
  }
}

/* 特殊动画效果 */
@keyframes levelComplete {
  0% { transform: scale(1) rotate(0deg); }
  25% { transform: scale(1.1) rotate(5deg); }
  50% { transform: scale(1.2) rotate(-5deg); }
  75% { transform: scale(1.1) rotate(5deg); }
  100% { transform: scale(1) rotate(0deg); }
}

.level-complete-overlay .overlay-content {
  animation: levelComplete 1s ease;
}

@keyframes gameStart {
  0% { opacity: 0; transform: translateY(-50px); }
  100% { opacity: 1; transform: translateY(0); }
}

.start-overlay .overlay-content {
  animation: gameStart 0.5s ease;
}

@keyframes achievementUnlock {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.achievement.unlocked {
  animation: achievementUnlock 0.5s ease;
}

/* 加载动画 */
@keyframes loading {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading {
  animation: loading 1s linear infinite;
}

/* 成功动画 */
@keyframes success {
  0% { transform: scale(1); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

.success {
  animation: success 0.6s ease;
}

/* 错误动画 */
@keyframes error {
  0% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
  100% { transform: translateX(0); }
}

.error {
  animation: error 0.5s ease;
}

/* 彩虹动画 */
@keyframes rainbow {
  0% { filter: hue-rotate(0deg); }
  100% { filter: hue-rotate(360deg); }
}

.rainbow {
  animation: rainbow 3s linear infinite;
}

/* 脉冲动画 */
@keyframes pulse {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.05); opacity: 0.8; }
  100% { transform: scale(1); opacity: 1; }
}

.pulse {
  animation: pulse 2s ease infinite;
}

/* 闪烁动画 */
@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0.3; }
}

.blink {
  animation: blink 1s ease infinite;
}

/* 弹跳动画 */
@keyframes bounce {
  0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
  40% { transform: translateY(-10px); }
  60% { transform: translateY(-5px); }
}

.bounce {
  animation: bounce 1s ease infinite;
}

/* 摇摆动画 */
@keyframes shake {
  0% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  50% { transform: translateX(5px); }
  75% { transform: translateX(-5px); }
  100% { transform: translateX(0); }
}

.shake {
  animation: shake 0.5s ease;
}

/* 旋转动画 */
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.spin {
  animation: spin 1s linear infinite;
}

/* 淡入动画 */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.fade-in {
  animation: fadeIn 0.5s ease;
}

/* 滑入动画 */
@keyframes slideIn {
  from { transform: translateX(-100%); }
  to { transform: translateX(0); }
}

.slide-in {
  animation: slideIn 0.5s ease;
}

/* 缩放动画 */
@keyframes zoomIn {
  from { transform: scale(0); }
  to { transform: scale(1); }
}

.zoom-in {
  animation: zoomIn 0.3s ease;
}

/* 高亮动画 */
@keyframes highlight {
  0% { background-color: transparent; }
  50% { background-color: rgba(255,215,0,0.3); }
  100% { background-color: transparent; }
}

.highlight {
  animation: highlight 1s ease;
}

/* 记录发光动画 */
@keyframes recordGlow {
  0%, 100% { 
    box-shadow: 0 0 5px rgba(255,215,0,0.5);
  }
  50% { 
    box-shadow: 0 0 20px rgba(255,215,0,0.8), 0 0 30px rgba(255,215,0,0.6);
  }
}

.record-glow {
  animation: recordGlow 2s ease infinite;
}
</style>